package com.zdxc.controller;


import com.zdxc.entities.Userinfo;
import com.zdxc.service.UserService;
import common.bean.Paging;
import common.bean.ResultJsonWrap;
import common.util.StringEncrypt;
import common.util.UserInfoUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/user/*")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getuser")
    @ResponseBody
    public Userinfo getUser(@RequestParam("userId") String userId){
       return  this.userService.findByKey(userId);
    }

    /**
     * 分页返回
     * @param userCode,userName
     * @param paging
     * @return
     */
    @RequestMapping("/getcondition")
    @ResponseBody
    public Paging getusers(@RequestParam(name="userCode",defaultValue="") String userCode,@RequestParam(name="userName",defaultValue="") String userName,
             Paging paging){
        Userinfo userinfo =new Userinfo();
        userinfo.setUserName(userName);
        userinfo.setUserCode(userCode);
        Paging resultPage =userService.findByCondition(userinfo,paging);
        return  resultPage;
    }


    @RequestMapping(value="/list")
    public String list(){
        return "user/list";
    }

    @RequestMapping(value="/add")
    public String add(){
        return "user/add";
    }



    @RequestMapping(value="/delete")
    @ResponseBody
    public ResultJsonWrap  delete(String userId){
        ResultJsonWrap resultJsonWrap = new ResultJsonWrap();
        int  result = userService.delete(userId);
         if (result>0)
            resultJsonWrap.setMsg("删除成功");
        else
            resultJsonWrap.setErrorMsg("添加失败");
        return resultJsonWrap;
    }


    @RequestMapping(value="/save")
    @ResponseBody
    public ResultJsonWrap  save(Userinfo user ){
        ResultJsonWrap resultJsonWrap = new ResultJsonWrap();
        Userinfo temp = userService.findUserByUsercode(user.getUserCode());
        if (temp!=null){
            resultJsonWrap.setErrorMsg("添加失败,用户名已存在");
            return resultJsonWrap;
        }
        int result = userService.insert(user);
        if (result>0)
            resultJsonWrap.setMsg("添加成功");
        else
            resultJsonWrap.setErrorMsg("添加失败");
        return resultJsonWrap;
    }


    /**
     * 用户登录
     * @param userCode
     * @param password
     * @return
     */
    @RequestMapping("/valid")
    @ResponseBody
    public ResultJsonWrap valid(@RequestParam("username") String userCode ,@RequestParam("password") String password){
        ResultJsonWrap resultJsonWrap = new ResultJsonWrap();
        Userinfo user = userService.findUserByUsercode(userCode);

        if (user==null ){
            resultJsonWrap.setErrorMsg("您输入的用户名不存在，请重新输入");
        }else
        if (!user.getPasswd().equals(StringEncrypt.encryptByMd5(password))){
            resultJsonWrap.setErrorMsg("您输入的密码错误，请重新输入");
        }else
        {
            resultJsonWrap.setData(user);
            resultJsonWrap.setMsg("正在登录");
            UserInfoUtils.setUser(user); //保存在Session中
        }
        return resultJsonWrap;

    }

    @RequestMapping(value="/pass")
    public String pass(@RequestParam("userId") String userId ,ModelAndView modelAndView){
        Userinfo user = userService.findByKey(userId);
        modelAndView.addObject("realName",user.getUserName());

        modelAndView.addObject("userId",userId);
        return "user/password";
    }


}
